#include <windows.h>

#pragma pack(push)
#pragma pack(1)
struct ThreadHeader 
{
	char     root[9];
	short    count;
	char     lastMAC[3];
};
#pragma pack(pop)

int CheckMACs_CompareRoot( char *root1, char * root2)
{
	int i;
	for ( i = 0; i < 9; i++)
	{
		if ( root1[i] > root2[i] ) return 1;
		if ( root1[i] < root2[i] ) return -1;
	}

	return 0;
}


int CheckMACs_CompareSub( char *sub1, char * sub2)
{
	int i;
	for ( i = 0; i < 3; i++)
	{
		if ( sub1[i] > sub2[i] ) return 1;
		if ( sub1[i] < sub2[i] ) return -1;
	}

	return 0;
}

int Char16To2Int( TCHAR c )
{
	int res;

	switch ( c )
	{
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9': res = c -'0'; break;
		case 'A': 
		case 'a': res = 10; break;
		case 'B': 
		case 'b': res = 11; break;
		case 'C': 
		case 'c': res = 12; break;
		case 'D': 
		case 'd': res = 13; break;
		case 'E': 
		case 'e': res = 14; break;
		case 'F': 
		case 'f': res = 15; break;
	}
	return res;
}
int Do_CheckMACs( TCHAR * mac, TCHAR * fileName)
{
	int result = 0;

	char m_root[9];
	char m_sub [3];
	char sub[3];
	int v,i, readed, res;
	
	struct ThreadHeader header;

	HANDLE hFile = CreateFile( fileName, GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if ( hFile != INVALID_HANDLE_VALUE )
	{
		SetFilePointer(hFile, 4*256, 0, FILE_BEGIN);

		for ( i = 0; i < 12; i++)
		{
			v = Char16To2Int(mac[i]);

			if ( i < 9 )
			{
				m_root[i] = (char)v;
			}
			else
			{
				m_sub[i-9] = (char)v;
			}
		}

		while (1)
		{
			ReadFile(hFile, &header, sizeof(struct ThreadHeader), &readed, NULL);
			if ( readed == sizeof(struct ThreadHeader) )
			{
				if ( CheckMACs_CompareRoot(header.root, m_root) != 0 )
				{
					SetFilePointer(hFile, header.count*3, 0, FILE_CURRENT);
					continue;
				}
				else
				{
					res = CheckMACs_CompareSub(header.lastMAC, m_sub);
					if ( res == 0 )
					{
						result = 1; 
						break;
					} else if ( res == -1 ) {
						result = 0; 
						break;
					} else {
						for ( i = 0; i < header.count; i++ )
						{
							ReadFile(hFile, sub, sizeof(sub), &readed, NULL);
							if ( readed == sizeof(sub) )
							{
								res = CheckMACs_CompareSub( m_sub, sub);
								if ( res == 0 )
								{
									result = 1;
									break;
								} else if ( res == -1) {
									result = 0;
									break;
								} else {
									continue;
								}
							}
						}
						break;
					}
				}
			} 
			else
			{
				break;
			}
		}
		CloseHandle(hFile);
	}
	return result;
}

void GetMACVersion( char * version, TCHAR * fileName)
{
	int readed;

	HANDLE hFile = CreateFile( fileName, GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if ( hFile != INVALID_HANDLE_VALUE )
	{
		ReadFile(hFile, version, 20, &readed, NULL);
		CloseHandle(hFile);
	}
}
